package com.txby.datasources;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.jdbc.datasource.lookup.DataSourceLookup;

/**
 * 动态数据源
 */
public class DynamicDataSource extends AbstractRoutingDataSource {
	/**
	 * 多线程管理
	 */
	// private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
	/**
	 * 自定义数据源集合
	 */
	private Map<String, DataSource> cusTargetDataSources=new HashMap();
	/**
	 * 默认数据源
	 */
	private DataSource cusDefaultTargetDataSource;

	public DynamicDataSource(DataSource defaultTargetDataSource, Map<String, DataSource> targetDataSources) {
		this.cusTargetDataSources.putAll(targetDataSources);
		this.cusDefaultTargetDataSource = defaultTargetDataSource;
		super.setDefaultTargetDataSource(defaultTargetDataSource);
		super.setTargetDataSources(new HashMap<>(targetDataSources));
		super.afterPropertiesSet();
	}

	@Override
	protected Object determineCurrentLookupKey() {
		return getDataSourceName();
	}

	public static void setDataSourceName(String dataSourceName) {
		// contextHolder.set(dataSource);
		DatabaseContextHolder.setDataSourceName(dataSourceName);
	}

	public static String getDataSourceName() {
		return DatabaseContextHolder.getDataSourceName();
		// return contextHolder.get();
	}

	public static void clearDataSourceName() {
		DatabaseContextHolder.clearDataSourceName();
		// contextHolder.remove();
	}

	@Override
	public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
		super.setDataSourceLookup(dataSourceLookup);
	}

	@Override
	public void setDefaultTargetDataSource(Object defaultTargetDataSource) {
		super.setDefaultTargetDataSource(defaultTargetDataSource);
	}

	@Override
	public void setTargetDataSources(Map targetDataSources) {
		super.setTargetDataSources(targetDataSources);
		// 重点触发后load所有数据源
		super.afterPropertiesSet();
	}

	/**
	 * @return cusTargetDataSources
	 */
	public Map<String, DataSource> getCusTargetDataSources() {
		return cusTargetDataSources;
	}

	/**
	 * @param cusTargetDataSources
	 *            要设置的 cusTargetDataSources
	 */
	public void setCusTargetDataSources(Map<String, DataSource> cusTargetDataSources) {
		this.cusTargetDataSources = cusTargetDataSources;
		this.setTargetDataSources(cusTargetDataSources);
	}

	/**
	 * 获取当前默认数据源
	 * 
	 * @return cusDefaultTargetDataSource
	 */
	public Object getCusDefaultTargetDataSource() {
		return cusDefaultTargetDataSource;
	}

	/**
	 * @param cusDefaultTargetDataSource
	 *            要设置的 cusDefaultTargetDataSource
	 */
	public void setCusDefaultTargetDataSource(DataSource cusDefaultTargetDataSource) {
		this.cusDefaultTargetDataSource = cusDefaultTargetDataSource;
		this.setDefaultTargetDataSource(cusDefaultTargetDataSource);
	}

	/**
	 * 添加数据源对象 @Title: addDataSource @Description:
	 * TODO(这里用一句话描述这个方法的作用) @param @param key @param @param dataSource 设定文件 @return
	 * void 返回类型 @throws
	 */
	public void addDataSourceBean(String key, DataSource dataSource) {
		this.cusTargetDataSources.put(key, dataSource);
		this.setCusTargetDataSources(this.cusTargetDataSources);
	}

	/**
	 * 移除数据源对象 @Title: removeDataSourceBean @Description:
	 * TODO(这里用一句话描述这个方法的作用) @param @param key 设定文件 @return void 返回类型 @throws
	 */
	public void removeDataSourceBean(Object key) {
		this.cusTargetDataSources.remove(key);
		this.setCusTargetDataSources(this.cusTargetDataSources);
	}

}
